package com.tarry.io.file.archive;

import lombok.extern.slf4j.Slf4j;

import java.util.Objects;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Slf4j
public class FileProcessMain {

    private final static String FILE_READ_PATH = "D:\\02_ebook\\";
    private final static String FILE_TMP_PATH = "D:\\ebook_test\\tmp_02";
    private final static String FILE_WRITE_PATH = "D:\\ebook_test\\ebook_bak\\";

//    private final static String FILE_READ_PATH = "/Users/tarry/Downloads/dian子shu";
//    private final static String FILE_WRITE_PATH = "/Users/tarry/Downloads/ebook";
//    private final static String FILE_TMP_PATH = "/Users/tarry/Downloads/tmp_01";

    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        int cpus = Runtime.getRuntime().availableProcessors();
        log.info("the thead cores {}", cpus);
        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(
                cpus,
                cpus * 2,
                10L,
                TimeUnit.SECONDS,
                // 阻塞双锁线程安全的队列，有界，最大长度为Integer.MAX_VALUE，可能会造成内存溢出
                new LinkedBlockingQueue<>(1024),
                // 自定义拒绝策略
                (runnable, queue) -> {
                    if (!queue.isShutdown()) {
                        try {
                            // put 队列满时，阻塞队列
                            queue.getQueue().put(runnable);
                        } catch (InterruptedException ex) {
                            throw new RuntimeException(ex);
                        }
                    }
                }
        );

        LinkedBlockingQueue<FileInfo> queue = new LinkedBlockingQueue<>(2048);

        ListFileProducer listFileProducer = new ListFileProducer(queue, FILE_READ_PATH);

        poolExecutor.submit(listFileProducer);

        while (true) {
            try {
                log.info("get queue value ~~~");
                // 当从队列中取不到对象时，进入等待状态，30秒后返回null
                FileInfo fileInfo = queue.poll(30L, TimeUnit.SECONDS);
                if (Objects.nonNull(fileInfo)) {
                    poolExecutor.submit( new FileCustomer(fileInfo,FILE_READ_PATH, FILE_TMP_PATH));
                }else {
                    log.info("the queue is empty, exist !");
                    break;
                }
            } catch (InterruptedException e) {
                log.error("do customer process error !", e);
                throw new RuntimeException(e);
            }
        }
        poolExecutor.shutdown();
        try {
            // 主线程进入等待状态，等待线程池中所有的线程运行完毕后，才继续执行。
            poolExecutor.awaitTermination(Integer.MAX_VALUE,TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        long end = System.currentTimeMillis();
        log.info("=============================================================");
        log.info("耗时： {} 分钟", (end - start) / 1000F /60 );
        log.info("=============================================================");

    }
}
